﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.委托;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmGlyxxModify:Form
    {
        #region 属性
        GlyDao glyDao = new GlyDao();
        Delegates delegates = new Delegates();
        private static FrmGlyxxModify Instance;
        public static bool IsModify = true;
        #endregion

        #region 构造器
        public FrmGlyxxModify()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(delegates_refreshLBEvent);
        }
        #endregion

        #region 事件
        public static FrmGlyxxModify GetInstance(Delegates delegates)
        {
            Instance = new FrmGlyxxModify();
            if (IsModify)
                Instance.BindingData();
            else
            {
                Instance.LblGetGlyCode.Text = Instance.glyDao.getMaxGlyCode();
                Instance.toolTip1.Dispose();
            }
            Instance.delegates = delegates;
            Instance.BringToFront();
            return Instance;
        }

        public static FrmGlyxxModify GetInstance()
        {
            Instance = new FrmGlyxxModify();
            Instance.BindingData();
            return Instance;
            
        }

        private void TstripBtnDone_Click(object sender, EventArgs e)
        {
            GlyEntity glyEntity = new GlyEntity();
            glyEntity.gly_zh = TxtGlyZh.Text;
            glyEntity.gly_pwd = TxtGlyPwd.Text;
            glyEntity.gly_name = TxtGlyName.Text;
            glyEntity.gly_sex = TxtGlySex.Text;
            glyEntity.gly_lxdh = TxtLxdh.Text;
            glyEntity.gly_lxdz = TxtLxdz.Text;
            glyEntity.gly_code = LblGetGlyCode.Text;
            if (IsModify)
            {
                glyDao.updateGlyxx(glyEntity);
                MessageBox.Show("修改成功");
            }
            else
            {
                String[] glyZh = glyDao.getGlyZh();
                for (int i = 0; i < glyZh.Length; i++)
                {
                    if (TxtGlyZh.Text == glyZh[i])
                    {
                        MessageBox.Show("账号已经存在");
                        return;
                    }
                }
                glyDao.insertGly(glyEntity);
                MessageBox.Show("注册成功");
            }
            if (delegates != null)
                delegates.doWork();
            this.Close();
        }

        private void TstripBtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtGlyPwd_Click(object sender, EventArgs e)
        {
            if (IsModify == true)
                FrmChangPwd.GetInstance(delegates).ShowDialog();
        }
        #endregion

        #region 方法
        void delegates_refreshLBEvent()
        {
            BindingData();
        }

        void BindingData()
        {
            TxtGlyName.Text = GlobalVar.GlyEntity.gly_name;
            TxtGlyPwd.Text = GlobalVar.GlyEntity.gly_pwd;
            TxtGlySex.Text = GlobalVar.GlyEntity.gly_sex;
            TxtGlyZh.Text = GlobalVar.GlyEntity.gly_zh;
            TxtLxdh.Text = GlobalVar.GlyEntity.gly_lxdh;
            TxtLxdz.Text = GlobalVar.GlyEntity.gly_lxdz;
            LblGetGlyCode.Text = GlobalVar.GlyEntity.gly_code;
        }
        #endregion
    }
}
